from logging import PlaceHolder
from fastapi import Depends, Request
from fastapi.responses import JSONResponse
from nicegui import Client, app, ui
from sqlalchemy.orm import Session
from sqlalchemy import select
from db.bs_crud import query_bs_by_name, get_bs_all, get_lte_freq_record
from db.bs_models import LTEFreqRecord, BaseStation
from fastapi import Request, Depends

# 修改前
# from db.db import get_db
# 修改后
from db.db import get_db
from mobile_frame import mobileframe
from ucontrols.add_lte_freq_record_dialog import AddLteFreqRecordDialog


class LteFreqRecordManager:
    def __init__(self, db, base_station_id: int) -> None:
        self.db = db
        self.page_title = "4G新增测试记录表"
        self.base_station: BaseStation = db.get(BaseStation, base_station_id)

    def show(self):
        ui.markdown(f"{self.base_station.name} 基站的4G LTE测试记录管理")
        with ui.row():
            base_stations = query_bs_by_name(
                self.db, self.base_station.name
            )  # query_bs_by_name(self.db, name="数字")
            options = {bs.id: bs.name for bs in base_stations}
            ui.select(
                options,
                with_input=True,
                label="选择基站",
                on_change=lambda e: (
                    ui.notify(f"已选择: {e.value} - {options[e.value]}"),
                    self.show_record(e.value),
                ),
            )
            add_bs_dlg = AddLteFreqRecordDialog(self.db, self.base_station.id)
            ui.button("新增", on_click=add_bs_dlg.show()).props("color=primary")

    @ui.refreshable
    # 为避免重定义内置函数 'id'，将参数名改为 'record_id'
    def show_record(self, bs_id: int):
        if bs_id:
            stmt = (
                select(LTEFreqRecord)
                .where(LTEFreqRecord.base_station_id == bs_id)
                .order_by(LTEFreqRecord.record_datetime.desc())
            )
            records = self.db.execute(stmt).scalars().all()
            ui.notify(records)
            for record in records:
                print(f"record: {record}")
                with ui.card().classes("w-full h-1/4 bg-sky-300 justify-center mt-2"):
                    ui.label(f"点位编号: {record.point_number}")
                    ui.label(f"与天线最小高度差（m）: {record.line_min}")
                    ui.label(f"监测结果1(V/m): {record.result_1}")
                    ui.label(f"监测结果2(V/m): {record.result_2}")
                    ui.label(f"监测结果3(V/m): {record.result_3}")
                    ui.label(f"监测结果4(V/m): {record.result_4}")
                    ui.label(f"监测结果5(V/m): {record.result_5}")
                    # ui.label(f"图片文件路径及名称: {record.image_path}")
                    ui.label(f"点位描述: {record.point_description}")
                    ui.label(f"经度(°): {record.longitude}")
                    ui.label(f"纬度(°): {record.latitude}")
                    ui.label(f"备注信息: {record.remarks}")
                    ui.label(f"与天线最小水平距离（m）: {record.line_max}")
                    # ui.label(f"监测结果平均值(V/m): {record.average_result}")
                    # ui.label(f"标准偏差: {record.standard_deviation}")
                    # ui.label(f"移动分频(V/m): {record.mobile_value}")
                    # ui.label(f"联通分频(V/m): {record.unicom_value}")
                    # ui.label(f"电信分频(V/m): {record.telecom_value}")
                    # ui.label(f"铁塔分频(V/m): {record.telegraph_value}")
                    # ui.label(f"其他分频(V/m): {record.other_value}")
                    # ui.label(f"是否天面: {record.is_roof}")
                    ui.label(f"检测时间: {record.record_datetime}")
                    ui.label(f"检测人: {record.recorder}")

    @ui.refreshable
    def show_records(self):
        pass


@ui.page("/mobile/lte")
def lte_freqrecordmmanage(db: Session = Depends(get_db)):
    """
    4G LTE测试记录管理
    """
    page = LteFreqRecordManager(db, 2)
    with mobileframe(page.page_title):
        page.show()
